﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.AdminCP
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUsername.Focus();
            if (Request.QueryString["Option"] != null)
                if (Request.QueryString["Option"].ToString() == "Logout")
                    Session.RemoveAll();
        }

       
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string txtPass = Functions.EncryptMd5(txtPassword.Text);
            if (txtUsername.Text.Equals("superadmin") && Functions.EncryptMd5(Functions.EncryptMd5(txtPassword.Text.Trim())).Equals("57D98F00C9F13A223D3F1A3A4C414FC4"))
            {
                Session["UsernameAd"] = "SuperAdmin";
                Session["UserID"] = "20142014";
                Session["UserType"] = "1";

                if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                {
                    Response.Redirect(Request.QueryString["ReturnUrl"]);
                }
                else
                {
                    Response.Redirect("/AdminCP/default.aspx");
                }
            }
            else
            {
                DataTable dtUser = new sysUserBL().SelectLogin(txtUsername.Text, txtPass);
                if (dtUser.Rows.Count > 0)
                {
                    Session["UsernameAd"] = dtUser.Rows[0]["Username"].ToString();
                    //  Session["FullName"] = dtUser.Rows[0]["FullName"].ToString();
                    Session["UserID"] = dtUser.Rows[0]["UserID"].ToString();
                    Session["UserType"] = dtUser.Rows[0]["UserType"].ToString();


                    if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                    {
                        Response.Redirect(Request.QueryString["ReturnUrl"]);
                    }
                    else
                    {
                        Response.Redirect("/admincp/default.aspx");
                    }
                }
                else
                {
                    Functions.Alert("Sai tên đăng nhập hoặc tài khoản đã bị khóa!!!", Request.Url.ToString());
                }
            }

        }
    }
}
